define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/finproduct/index',
                    add_url: 'store/finproduct/add',
                    edit_url: 'store/finproduct/edit',
                    del_url: 'store/finproduct/del',
                    multi_url: 'store/finproduct/multi',
                    import_url: 'store/finproduct/import',

                    table: 'finproduct',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                searchFormTemplate: 'customformtpl',

                columns: [
                    [
                        {field: 'id', title: 'id'},

                        {field: 'cname', title: __('Cname')},
                        {field: 'uname', title: __('Uname')},
                        {field: 'price', title: __('Price')},

                        {field: 'ctime', title: __('Ctime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'remark', title: __('Remark')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams:function (params) {
                    var cname = $("#cname").val();
                    var first = $(".first").val();
                    var second = $(".second").val();
                    var three = $(".three").val();

                    params.filter = {};

                    if(cname!='') params.filter.cname = cname;
                    if(first > 0) params.filter.first = first;
                    if(second > 0) params.filter.second = second;
                    if(three > 0) params.filter.three = three;

                    return {filter:JSON.stringify(params.filter),op:JSON.stringify(params.op),offset:JSON.stringify(params.offset),limit:JSON.stringify(params.limit)};
                }

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

            $("#seles").click(function () {
                layer.open({
                    type: 2,
                    title: '半成品选择',
                    area: ['80%', '80%'],
                    content: '/admin/store/finproduct/sele/' //iframe的url
                });

            });
        },
        sele:function(){
            var bcp = parent.$("#bcp").val();
            if (bcp!='' && bcp!=undefined){
               var arr = bcp.split(',');
               for(var i=0;i<arr.length;i++){
                    $("#c"+arr[i]).css({'backgroundColor':'#999'});
               }
            }
            Controller.api.bindevent();
        },
        edit: function () {
            $("#seles").click(function () {

                layer.open({
                    type: 2,
                    title: '半成品选择',
                    area: ['80%', '80%'],
                    content: '/admin/store/finproduct/sele/ids/'+$(this).attr('data') //iframe的url
                });

            });
            Controller.api.bindevent();

        },
      select: function () {
        // 初始化表格参数配置
        Table.api.init({
          extend: {
            index_url: 'store/finproduct/select',
          }
        });

        var table = $("#table");

        // 初始化表格
        table.bootstrapTable({
          url: $.fn.bootstrapTable.defaults.extend.index_url,
          sortName: 'id',
          columns: [
            [
              {checkbox: true},
              {field: 'id', title: __('Id')},
              {field: 'cname', title: __('Cname')},
              {field: 'price', title: __('Price')},
              {field: 'storenum', title: __('库存数量')},
              {field: 'storecall', title: __('库存报警')},
              {field: 'uname', title: __('Uname')},
              {field: 'ctime', title: __('Ctime'), operate:false},
              {field: 'remark', title: __('Remark')},
              {field: 'operate', title: __('Operate'), events: {
                  'click .btn-chooseone': function (e, value, row, index) {
                    Fast.api.close({ids: row.id, goods_name: row.cname,storenum:row.storenum,price:row.price, multiple: false});
                  },
                }, formatter: function () {
                  return '<a href="javascript:;" class="btn btn-danger btn-chooseone btn-xs"><i class="fa fa-check"></i> ' + __('Choose') + '</a>';
                }}
            ]
          ],
          queryParams: function (params) {
            var goods_ids = Backend.api.query('goods_ids');
            if(goods_ids){
              params.filter = JSON.stringify({id: goods_ids, status: 1});
              params.op = JSON.stringify({id: 'not in', status: '='});
              return params;
            }

          },
        });

        // 选中多个
        $(document).on("click", ".btn-choose-multi", function () {
          var idsArr = new Array();
          $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
            idsArr.push(j);
          });
          var multiple = Backend.api.query('multiple');
          multiple = multiple == 'true' ? true : false;
          Fast.api.close({list: idsArr, multiple: multiple});
        });

        // 为表格绑定事件
        Table.api.bindevent(table);
      },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }

    };
    return Controller;
});
function haha(id,cname,classif,obj,storm){


    var ultable = parent.$("#ultable"),hidarr=parent.$("#bcp");
    var bcp=hidarr.val();
    var sto=hidarr.attr('data-value');

    $(obj).parent().parent('tr').css({'backgroundColor':'#999'});

    if (bcp=='' || bcp==null){
        var sj = id;

    } else{
        var sp = bcp.split(',');
         for(var i=0;i<sp.length;i++){
            if(sp[i]==id){
                return;
            }
        }
        var sj = bcp+','+id;

    }

    hidarr.val(sj);


    var html = '<tr id="h'+id+'"><td style="text-align: center; vertical-align: middle; ">'+id+'</td><td style="text-align: center; vertical-align: middle; ">'+cname+'</td><td style="text-align: center; vertical-align: middle; ">'+classif+'</td><td>'+storm+'</td><td style="text-align: center; vertical-align: middle; "><a onclick="dele('+id+')" style="cursor: pointer"class="btn btn-xs btn-success btn-editone"><i class="fa fa-trash"></i></a></td></tr>';

    ultable.append(html);



};
function dele(id,obj) {
    var bcp=$("#bcp").val();
    var sp = bcp.split(',');
    var arr=[];
    for(var i=0;i<sp.length;i++){
        if(sp[i]!=id){
            arr.push(sp[i]);
        }
    }
    if (arr.length >0){
        $("#bcp").val(arr.join(','));
    }else{
        $("#bcp").val('');
    }
    $("#h"+id).remove();
}